/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.transfer.backing;

import com.transfer.Reservation;
import com.transfer.Room;
import com.transfer.dao.RoomsDAOLocal;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class RoomsBackingBean implements Serializable {
    private static final Logger log = Logger.getLogger(RoomsBackingBean.class.getName());
    private Room selectedRoom;
    private Reservation selectedReservation;

    @EJB
    private RoomsDAOLocal roomsDao;

    public RoomsBackingBean() {
    }

    public void createRooms() {
        if(roomsDao.getAllRooms().isEmpty()) {
            roomsDao.createRoom("Meeting room 1", 100);
            roomsDao.createRoom("Meeting room 2", 200);
            roomsDao.createRoom("Meeting room 3", 300);
        }
    }

    public Room getSelectedRoom() {
        return selectedRoom;
    }

    public void setSelectedRoom(Room selectedRoom) {
        this.selectedRoom = selectedRoom;
    }

    public void updateRoom() {
        log.log(Level.FINE, "updateRoom(): {0}", getSelectedRoom());
        getRoomsDao().updateRoom(getSelectedRoom());
    }

    public RoomsDAOLocal getRoomsDao() {
        createRooms();
        return roomsDao;
    }

    public void setRoomsDao(RoomsDAOLocal roomsDao) {
        this.roomsDao = roomsDao;
    }

    public boolean isRoomSelected() {
        return null != getSelectedRoom();
    }

    public Reservation getSelectedReservation() {
        return selectedReservation;
    }

    public void setSelectedReservation(Reservation selectedReservation) {
        log.log(Level.INFO, "Set selected reservation: {0}", selectedReservation);
        this.selectedReservation = selectedReservation;
    }

    public boolean isReservationSelected() {
        return null != getSelectedReservation();
    }

    public void newRoom() {
        getRoomsDao().createRoom("<New room>", 0);
    }

    public void updateReservation() {
        getRoomsDao().updateReservation(getSelectedReservation());
    }

    public void newReservation() {
        getRoomsDao().createReservation(getSelectedRoom());
    }
}
